<template>
  <el-dialog title="上传图片" :visible.sync="visible" width="1024px">
    <div class="app-page">
      <el-upload
        ref="upload"
        class="avatar-uploader"
        action=""
        accept=".bmp, .gif, .jpg, .jpeg, .png, image/bmp, image/gif, image/jpg, image/jpeg, image/png"
        :show-file-list="false"
        :auto-upload="false"
        :on-change="(res, file)=>handleChange(res, file, 'image1')"
      >
        <img v-if="temp.image1" :src="temp.image1" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon" />
      </el-upload>
      <el-upload
        ref="upload"
        class="avatar-uploader"
        action=""
        accept=".bmp, .gif, .jpg, .jpeg, .png, image/bmp, image/gif, image/jpg, image/jpeg, image/png"
        :show-file-list="false"
        :auto-upload="false"
        :on-change="(res, file)=>handleChange(res, file, 'image2')"
      >
        <img v-if="temp.image2" :src="temp.image2" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon" />
      </el-upload>
      <el-upload
        ref="upload"
        class="avatar-uploader"
        action=""
        accept=".bmp, .gif, .jpg, .jpeg, .png, image/bmp, image/gif, image/jpg, image/jpeg, image/png"
        :show-file-list="false"
        :auto-upload="false"
        :on-change="(res, file)=>handleChange(res, file, 'image3')"
      >
        <img v-if="temp.image3" :src="temp.image3" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon" />
      </el-upload>
      <el-upload
        ref="upload"
        class="avatar-uploader"
        action=""
        accept=".bmp, .gif, .jpg, .jpeg, .png, image/bmp, image/gif, image/jpg, image/jpeg, image/png"
        :show-file-list="false"
        :auto-upload="false"
        :on-change="(res, file)=>handleChange(res, file, 'image4')"
      >
        <img v-if="temp.image4" :src="temp.image4" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon" />
      </el-upload>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.success" type="primary" @click="success">
        保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { queryGoodsDefaultData, queryUploadGoodsImage } from '@/api'
  import { mapGetters } from 'vuex'
  import { imgBaseUrl } from '@/utils/config'

  export default {
    name: 'upload-images',
    data() {
      return {
        visible: false,
        row: {},
        image1File: null,
        image2File: null,
        image3File: null,
        image4File: null,
        temp: {
          image1: '',
          image2: '',
          image3: '',
          image4: ''
        },
        qiniuData: { // 这里是直接绑定data的值
          key: '', // 图片名字处理
          token: '', // 七牛云token
          data: {},
          bucket: ' '
        }
      }
    },
    computed: {
      ...mapGetters([
        'userInfo'
      ])
    },
    methods: {
      async open(row) {
        this.row = row
        await this.getDefaultData(row)
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
        })
      },
      // 获取默认图片
      async getDefaultData(temp) {
        const res = await queryGoodsDefaultData({
          goodsId: temp.goodsId
        })
        if (res.data.goodsInfo.imageName) {
          if (res.data.goodsInfo.imageName.split(';').length > 0) {
            const arr = res.data.goodsInfo.imageName.split(';')
            arr.forEach((item, index) => {
              this.temp[`image${index + 1}`] = imgBaseUrl + item
            })
          }
        }
      },
      handleChange(res, file, name) {
        this.temp[name] = URL.createObjectURL(res.raw)
        this[`${name}File`] = res.raw
      },
      async success() {
        const files = []
        if (this.image1File) files.push(this.image1File)
        if (this.image2File) files.push(this.image2File)
        if (this.image3File) files.push(this.image3File)
        if (this.image4File) files.push(this.image4File)
        await queryUploadGoodsImage({
          goodsId: this.row.goodsId,
          files
        })
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '上传成功',
          type: 'success',
          duration: 2000
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.app-page {
  display: flex;
  justify-content: space-evenly;
  /deep/ .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
  }
}
.avatar-uploader .el-upload {
  position: relative;
  overflow: hidden;
  cursor: pointer;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  width: 178px;
  height: 178px;
  font-size: 28px;
  line-height: 178px;
  color: #8c939d;
  text-align: center;
}
.avatar {
  display: block;
  width: 178px;
  height: 178px;
}
</style>
